﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NETMovie.Entities;
using NETMovie.Repositories.EF;
using NETMovie.Repositories;

namespace NETMovie.BLL
{
    public class AccountService
    {
        public List<string> Errors
        {
            get;
            set;
        }

        public AccountService()
        {
            Errors = new List<string>();
        }

        public User GetUser(string username)
        {
            NETMovie.Repositories.RepositoryFactory factory = RepositoryHelper.GetFactory(Repositories.RepositoryTypes.EF);

            User user = null;
            using (IUserRepository repo = factory.GetUserRepository())
            {
                user = repo.GetUser(username);
                if (user == null)
                {
                    Errors.Add("The username does not exist in the system");
                    return user;
                }
            }

            return user;
        }

        public bool UpdateUser(User user)
        {
            NETMovie.Repositories.RepositoryFactory factory = RepositoryHelper.GetFactory(Repositories.RepositoryTypes.EF);

            using (IUserRepository repo = factory.GetUserRepository())
            {
                // Check inputs
                if (!user.IsValid)
                {
                    Errors.AddRange(user.GetRuleViolations());
                    return false;
                }

                // Check if the user exists in the DB.
                if (repo.GetUser(user.UserName) == null)
                {
                    Errors.Add("The username does not exist in the system");
                    return false;
                }

                return repo.UpdateUser(user);
            }
        }

        public bool RegisterUser(User user)
        {
            NETMovie.Repositories.RepositoryFactory factory = RepositoryHelper.GetFactory(Repositories.RepositoryTypes.EF);

            using (IUserRepository repo = factory.GetUserRepository())
            {
                // Check inputs
                if (!user.IsValid)
                {
                    Errors.AddRange(user.GetRuleViolations());
                    return false;
                }

                // Check if the user exists in the DB.
                if (repo.GetUser(user.UserName) != null)
                {
                    Errors.Add("The username exists in the system");
                    return false;
                }

                // Create user in the DB
                return repo.CreateUser(user);
            }
        }

        public Country[] GetCountries()
        {
            Country[] countries = null;
            NETMovie.Repositories.RepositoryFactory factory = RepositoryHelper.GetFactory(Repositories.RepositoryTypes.EF);

            using (IUserRepository repo = factory.GetUserRepository())
            {
                countries = repo.GetCountries();
            }

            return countries;
        }
    }
}
